package com.al.filter;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import com.al.model.User;
import com.al.service.LoginService;

@WebFilter("/login")
public class UserInfoFilter implements Filter{
	@Override
	public void doFilter(ServletRequest req, ServletResponse rep, FilterChain chain)
			throws IOException, ServletException {
		String name = req.getParameter("username");
		String password = req.getParameter("password");
		LoginService ls = new LoginService();
		User user = null;
		try {
			user = ls.login(name, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(user.getName().equals("admin") ) {
			//是管理员账户进入管理员专属页面
			req.setAttribute("user", user);
			req.getRequestDispatcher("admin.jsp").forward(req, rep);
		}else {
			chain.doFilter(req, rep);
		}
	}
}
